﻿//using System;
//using System.Collections.Generic;
//using System.Linq;


//namespace Data.UserDb
//{
//    public static class AccountActions
//    {
//        /// <summary>
//        /// 帐户充值
//        /// </summary>
//        /// <param name="userId">用户Id</param>
//        /// <param name="username">用户名</param>
//        /// <param name="fromOrderId">付费的订单Id</param>
//        /// <param name="amount">充值金额</param>
//        /// <returns>充值的日志ID</returns>
//        public static Int64 AddVs(Int64 userId, String username, String fromOrderId, Int32 amount)
//        {
//            if(String.IsNullOrEmpty(fromOrderId)) throw new Exception("错误的订单来源");
//            if (amount <= 0) throw new Exception("错误的充值金额");

//            using (var ctxt = new UserAccountContext())
//            {
//                //获取用户帐号
//                var account = (from u in ctxt.Account where u.UId == userId select u).Take(1);
//                Account acc;
//                if(account.ToArray().Count() == 0)
//                {//帐号不存在，则添加新帐号
//                    acc = new Account
//                                  {
//                                      Amount = amount,
//                                      CreateTime = DateTime.Now,
//                                      UserName = username,
//                                      UId = userId,
//                                      UpdateTime = DateTime.Now
//                                  };
//                    ctxt.Account.InsertOnSubmit(acc);
//                }
//                else
//                {//帐号存在，更新帐户余额
//                    acc = account.ToArray()[0];
//                    acc.UpdateTime = DateTime.Now;
//                    acc.Amount = acc.Amount + amount;
//                }

//                //检查订单号是否已经存在
//                var checkOrderId =
//                    (from orderLog in ctxt.TransLogs where orderLog.OrderId.Equals(fromOrderId) select orderLog).Count();
//                if (checkOrderId > 0) throw new Exception("订单号已经处理，请不要重复提交");

//                var transinfos = (from trans in ctxt.TransactionType where trans.TransTypeId == 1 select trans).Take(1);
//                if (transinfos.ToArray().Count() == 0) throw new Exception("错误的事务类型丢失");
//                //记录日志
//                TransactionType ttype = transinfos.ToArray()[0];
//                var log = new TransLogs
//                                    {
//                                        Amount = amount,
//                                        OrderId = fromOrderId,
//                                        CreateTime = DateTime.Now,
//                                        Note = String.Format(ttype.TransTypeTemplate, username, amount),
//                                        UserName = username,
//                                        UserId = userId,
//                                        TransTypeId = ttype.TransTypeId,
//                                        TransTypeName = ttype.TransTypeName
//                                    };
//                ctxt.TransLogs.InsertOnSubmit(log);
//                ctxt.SubmitChanges();
//                return log.TransId;
//            }
//        }


//        /// <summary>
//        /// 获取用户帐户信息
//        /// </summary>
//        /// <param name="userid">用户ID</param>
//        /// <returns>用户账务信息</returns>
//        public static Account ViewUserAccount(Int64 userid)
//        {
//            using(var context = new UserAccountContext())
//            {
//                var acc = from a in context.Account where a.UId == userid select a;
//                if(acc.ToArray().Count() == 0) return null;
//                return acc.ToArray()[0];
//            }
//        }

//        /// <summary>
//        /// 获取用户的充值和消费记录
//        /// </summary>
//        /// <param name="userid">用户id</param>
//        /// <param name="startDate">查询开始时间</param>
//        /// <param name="endDate">查询结束时间</param>
//        /// <returns>消费记录集合</returns>
//        public static IEnumerable<TransLogs> ViewTransLogs(Int64 userid, DateTime startDate, DateTime endDate)
//        {
//            using(var context = new UserAccountContext())
//            {
//                var logs = from log in context.TransLogs
//                           where
//                               log.CreateTime.GetValueOrDefault().Date >= startDate &&
//                               log.CreateTime.GetValueOrDefault().Date <= endDate
//                           select log;
//                return logs;
//            }
//        }


//        /// <summary>
//        /// 扣款接口
//        /// </summary>
//        /// <param name="userId">用户Id</param>
//        /// <param name="username">用户名</param>
//        /// <param name="fromOrderId">扣款订单来源</param>
//        /// <param name="amount">扣款金额</param>
//        /// <returns>日志ID</returns>
//        public static Int64 SubVs(Int64 userId, String username, String fromOrderId, Int32 amount)
//        {
//            if (String.IsNullOrEmpty(fromOrderId)) throw new Exception("错误的订单来源");
//            if(amount > 0) amount = -amount;

//            using(var context = new UserAccountContext())
//            {
//                var account = (from acc in context.Account where acc.UId == userId select acc).Take(1);
//                if (account.ToArray().Count() == 0) throw new Exception("余额不足");

//                var tmpAccount = account.ToArray()[0];
//                if (Math.Abs(amount) < tmpAccount.Amount) throw new Exception("余额不足");

//                tmpAccount.Amount += amount;
//                tmpAccount.UpdateTime = DateTime.Now;

//                //检查订单号是否已经存在
//                var checkOrderId =
//                    (from orderLog in context.TransLogs where orderLog.OrderId.Equals(fromOrderId) select orderLog).Count();
//                if (checkOrderId > 0) throw new Exception("订单号已经处理，请不要重复提交");

//                var transinfos = (from trans in context.TransactionType where trans.TransTypeId == 2 select trans).Take(1);
//                if (transinfos.ToArray().Count() == 0) throw new Exception("错误的事务类型丢失");

//                //记录日志
//                TransactionType ttype = transinfos.ToArray()[0];
//                var log = new TransLogs
//                {
//                    Amount = amount,
//                    OrderId = fromOrderId,
//                    CreateTime = DateTime.Now,
//                    Note = String.Format(ttype.TransTypeTemplate, username, amount),
//                    UserName = username,
//                    UserId = userId,
//                    TransTypeId = ttype.TransTypeId,
//                    TransTypeName = ttype.TransTypeName
//                };
//                context.TransLogs.InsertOnSubmit(log);
//                context.SubmitChanges();
//                return log.TransId;
//            }
//        }


//    }

//    public enum TransType
//    {
//        AddVs = 1,
//        SubVs = 2
//    }
//}
